package com.ajmd.onlineexam.exception;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.enumeration.CodeMsgEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author ajmd
 * @since 2023/3/8 16:14
 */
@ControllerAdvice
public class ExceptionControllerAdvice {
//    @ExceptionHandler(RuntimeException.class)
//    public String handleException(RuntimeException e, Model model){
//        model.addAttribute("error",e.getMessage());
//        return "errorView";
//    }


    @ExceptionHandler(Exception.class)
    public CommonResult exceptionHandler(Exception e){
        //自定义异常类型(业务相关)
        if(e instanceof AppException){
            AppException appException = (AppException)e;
            return CommonResult.error(appException.getCode(),appException.getMessage());
        }
        //非业务异常
        return CommonResult.error(CodeMsgEnum.SERVICE_EXCEPTION);
    }
}
